package javabase.net.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClient {

	public static void main(String[] args) {
		System.out.println("客户端连接开始。。。");
		Socket socket = null;
		OutputStream os = null;
		PrintWriter pw = null;
		try {
			socket = new Socket("127.0.0.1", 9999);
			os = socket.getOutputStream();
			pw = new PrintWriter(os, true);
			pw.println("hello, 我是客户端");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (socket != null && !socket.isInputShutdown()) {
				try {
					socket.shutdownInput();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if (socket != null && !socket.isOutputShutdown()) {
				try {
					socket.shutdownOutput();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if (socket != null && socket.isConnected()) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
